#-----------------------------------------------------------
# officedocs.pl
# Plugin for Registry Ripper 
#
# Change history
#
#
# References
#
# 
# copyright 2008 H. Carvey
#-----------------------------------------------------------
package office;
use strict;
use DateTime::Format::WindowsFileTime;

require "functions.pl";

my %config = (hive          => "NTUSER\.DAT",
              hasShortDescr => 1,
              hasDescr      => 0,
              hasRefs       => 0,
              osmask        => 22,
              version       => qq(can read office 07 now));

sub getConfig{return %config}
sub getShortDescr {
	return "Gets contents of user's Office doc MRU keys";	
}
sub getDescr{}
sub getRefs {}
sub getHive {return $config{hive};}
sub getVersion {return $config{version};}
sub getWinTS {
	my $data = $_[0];
	my $winTS;
	(my $prefix, my $suffix) = split(/\*/,$data);
	if ($prefix =~ /\[.{9}\]\[T(.{16})\]/) {
		$winTS = $1;
	}	
	#::rptMsg($winTS)."\n";
	my $dateTime = (DateTime::Format::WindowsFileTime->parse_datetime($winTS));
	#return $dateTime;
	$dateTime =~ s/T/_/;
	(my $year, my $mon, my $other) = split(/-/, $dateTime);
	$dateTime = $year."/".$mon."/".$other;
	#(my $date, my $time) = split(/\s+/, $dateTime);
	return ($dateTime, $suffix);
}

my $VERSION = getVersion();

sub pluginmain {
	my $class = shift;
	my $ntuser = shift;
#	::logMsg("Launching officedocs v.".$VERSION);
	my $reg = Parse::Win32Registry->new($ntuser);
	my $root_key = $reg->get_root_key;
# First, let's find out which version of Office is installed
	my $version;
	my $tag = 0;
	my @versions = ("7\.0","8\.0", "9\.0", "10\.0", "11\.0","12\.0","14\.0");
	foreach my $ver (@versions) {
		my $key_path = "Software\\Microsoft\\Office\\".$ver;
		#."\\Common\\Open Find";
		if (defined($root_key->get_subkey($key_path))) {
			$version = $ver;
			$tag = 1;
		}
	}
	
	if ($tag) {
		#::logMsg("MSOffice version ".$version." located.");
		my $key_path = "Software\\Microsoft\\Office\\".$version;	                 
		my $of_key = $root_key->get_subkey($key_path);
		if ($of_key) {
			my @types = ("Word", "Excel", "PowerPoint");
			foreach my $type (@types){
				my $tmp_path = $type."\\File MRU";
				
				my $tmp_key = $of_key->get_subkey($tmp_path);
				#::logMsg("key is ".$tmp_key);
				if($tmp_key){
					my @vals = $tmp_key->get_list_of_values();
					if (scalar(@vals) > 0) {
						my %files;
# Retrieve values and load into a hash for sorting			
						foreach my $v (@vals) {
							my $val = $v->get_name();
							#print ("val is ".$val."\n");
							if ($val eq "Max Display") { next; }
							(my $dateTime, my $path) = getWinTS($v->get_data());  
							#my $path = $v->get_data();
							#print("data is ".$data."\n");
							my $tag = (split(/Item/,$val))[1];
							#print("tag is ".$tag."\n");
							#$files{$tag} = $val.":".$data;
							my $host='';
							my $user='';
							my $action=$type;
							::rptMsg(UTC_to_UTF8($dateTime).",REG,$host,$user,$action,$path");
						}
# Print sorted content to report file	
=output	
						foreach my $u (sort {$a <=> $b} keys %files) {
							my ($val,$data) = split(/:/,$files{$u},2);
							::rptMsg("  ".$val.",".$data);
						}
=cut
					}
					else {
						::logMsg($key_path.$type." has no values.");
					}
					#my $key_in = $of_key->get_subkey($type);
					#::rptMsg("key_in is ".$key_in);
					#my $tvalue = $key_in->get_value("File MRU");#->get_data();
					#::rptMsg("value is ".$tvalue);
					#my @data = split(/\00/,$value);
					#map{::rptMsg("$_");}@data;
					
					#::rptMsg();
				}else{
					::logMsg("Could not access ".$type."\n");
				}
			}
		}
	}
	else {
		::logMsg("MSOffice version not found.");
		#::rptMsg("MSOffice version not found.");
	}
}

1;